package com.grupoconstruobras.sigo.core.config;

import com.grupoconstruobras.sigo.ServiceException;
import com.grupoconstruobras.sigo.general.ICrudManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.ejb.Stateful;
import javax.ejb.StatefulTimeout;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.dozer.DozerBeanMapper;

@StatefulTimeout(unit = TimeUnit.MINUTES, value = 30)
@Stateful(name = "paramConfigAppBussines", mappedName = "ejb/bussines/paramConfigAppBussines")
@EJB(beanInterface = IParamConfigAppBussines.class, beanName = "paramConfigAppBussines", name = "ejb/bussines/paramConfigAppBussines")
public class ParamConfigAppBussinesEjb implements IParamConfigAppBussines {

	private static Logger LOG = Logger
			.getLogger(ParamConfigAppBussinesEjb.class.getName());
	private ParametroConfigAppDto paramConfigAppDto;
	private ParametroConfigApp parametroConfigApp;
	private DozerBeanMapper mapper;

	@EJB(beanInterface = ICrudManager.class, beanName = "parametroConfigAppManager")
	private ICrudManager<Long, ParametroConfigApp> parametroConfigAppCrud;

	@PersistenceContext(unitName = "default")
	private EntityManager entityManager;

	public ParamConfigAppBussinesEjb() {
		List<String> mappingFiles = new ArrayList<String>();
		mappingFiles.add("/META-INF/configuracionapp-mapping.xml");
		mapper = new DozerBeanMapper(mappingFiles);
	}

	/******************** parámetros configuración APP ****************************/
	@Override
	public ParametroConfigAppDto newInstance() {
		this.paramConfigAppDto = new ParametroConfigAppDto();
		return paramConfigAppDto;
	}

	@Override
	public ParametroConfigAppDto getCurrent() {
		return paramConfigAppDto;
	}

	@Override
	public ParametroConfigAppDto save() throws ServiceException {
		try {
			parametroConfigApp = mapper.map(paramConfigAppDto,
					ParametroConfigApp.class, "DetalleParamConfigApp");
			if (parametroConfigApp.getId() == null) {
				parametroConfigApp = parametroConfigAppCrud
						.save(parametroConfigApp);
				paramConfigAppDto.setId(parametroConfigApp.getId());
			} else {
				parametroConfigApp = parametroConfigAppCrud.merge(
						parametroConfigApp.getId(), parametroConfigApp);
			}
			return paramConfigAppDto;
		} catch (Exception e) {
			LOG.severe(e.getMessage());
			throw new ServiceException("Ocurrio un error desconocido");
		}
	}

	@Override
	public void delete(Long id) throws ServiceException {
		try {
			parametroConfigAppCrud.remove(id);
		} catch (Exception e) {
			LOG.severe(e.getMessage());
			throw new ServiceException("Ocurrio un error desconocido");
		}
	}

	@Override
	public ParametroConfigAppDto get(Long id) throws ServiceException {
		try {
			parametroConfigApp = parametroConfigAppCrud.getById(id);
			paramConfigAppDto = mapper.map(parametroConfigApp,
					ParametroConfigAppDto.class, "DetalleParamConfigApp");
			return paramConfigAppDto;
		} catch (Exception e) {
			LOG.severe(e.getMessage());
			throw new ServiceException("Ocurrio un error desconocido");
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public ParametroConfigAppDto getByName(String name) throws ServiceException {
		String stringQuery = "SELECT d FROM ParametroConfigApp d"
				+ " where d.parametro =:name";
		List<ParametroConfigApp> list = entityManager.createQuery(stringQuery)
				.setParameter("name", name).getResultList();
		if (list != null && list.size() > 0) {
			paramConfigAppDto = mapper.map(list.get(0),
					ParametroConfigAppDto.class, "DetalleParamConfigApp");
			return paramConfigAppDto;
		}
		return null;
	}

}
